export default {
	data() {
		return {
			active: 0,
			list: [
				{
					cid: '0',
					label: '推荐',
					loading: false, // 控制loadMore显示
					finished: false, // 控制是否加载更多商品
					empty: false, // 控制是否为空数据
					page: 1,
					goodsList: []
				}
			],
			params: {
				page: 1,
				size: 20,
				order: '',
				sort: ''
			},
			pagination: {},
			service: null,
			isShowSearch: false,
			isShowScrollTop: false,
			isShowRecommednGoods: true
		};
	},
	created() {},
	methods: {
		// 加载数据
		async loadMore(item, operate) {
			// 从页面传入自定义处理方式
			if (!item.empty && !item.finished) {
				this.params.page = item.page;

				await this.beforeLoad(item);

				item.loading = true;
				// 触发高度更新
				if (this.$refs.tabs) {
					this.$refs.tabs.doLayout();
				}
				
				// 首页推荐商品
				// isShowRecommednGoods 判断掉上拉加载也会调取的方法
				if (this.isShowRecommednGoods) {
					if (this.showRecommednGoods) {
						if (this.active === 0) {
							await this.$service.shopping.goods
							.buyBackGoodsList({
								page: 1,
								size: 5
							})
							.then(res => {
								item.goodsList =  item.goodsList.concat(res.list);
							})
						}
					}
				}

				let res = await this.service;
				item.goodsList = item.goodsList.concat(res.list);
				if (res.subData) {
					item.subData = res.subData;
				}
				this.pagination = res.pagination;
				// 加载状态结束
				item.loading = false;
				uni.hideLoading();
				item.page += 1;

				// 刷新列表
				if (operate === 'pullDown') {
					uni.showToast({
						title: '刷新成功',
						icon: 'none',
						duration: 2000
					});
					uni.stopPullDownRefresh();
				}

				item.loading = false;

				// 暂无数据
				if (item.goodsList.length === 0) {
					item.empty = true;
					item.page = 1;
				} else if (item.goodsList.length >= res.pagination.total) {
					// 数据全部加载完成
					item.finished = true;
				}
				// 触发高度更新
				if (this.$refs.tabs) {
					this.$refs.tabs.doLayout();
				}

				// 回调回去 首页 函数能获取到list
				// if (this.refreshGoods) {
				// 	this.refreshGoods(item.goodsList, this.active);
				// }
				
				if (this.afterLoad) {
					this.afterLoad();
				}
			}
		},
		// 刷新
		onRefresh(item, operate) {
			item.goodsList = [];
			item.loading = true;
			item.finished = false;
			item.empty = false;
			item.page = 1;
			this.loadMore(item, operate);
		},
		// 切换加载
		tabChange(index, top) {
			// 触发高度更新
			if (this.$refs.tabs) {
				this.$refs.tabs.doLayout();
			}
			uni.pageScrollTo({
				scrollTop: top || 0,
				duration: 0
			});
			this.active = index;
			if (this.list[index].goodsList.length === 0) {
				this.loadMore(this.list[index]);
			}

			// 首页 切换 清除定时器
			if (this.refreshGoods) {
				this.clearRefresh();
			}
		},
		// 回到顶部
		backTop() {
			uni.pageScrollTo({
				scrollTop: 0,
				duration: 0
			});
		}
	},
	onPageScroll(e) {
		if (e.scrollTop >= 146) {
			this.isShowSearch = true;
		} else {
			this.isShowSearch = false;
		}
		if (e.scrollTop > 340) {
			this.isShowScrollTop = true;
		} else {
			this.isShowScrollTop = false;
		}
	},
	// 下拉刷新
	onPullDownRefresh() {
		this.isShowRecommednGoods = true;
		this.onRefresh(this.list[this.active], 'pullDown');
	},
	// 上拉加载
	onReachBottom() {
		if (!this.list[this.active].loading) {
			this.loadMore(this.list[this.active]);
		}
		this.isShowRecommednGoods = false;
	}
};
